package com.itheima.sh.b_login_filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/hack.html")
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //如果name，passw不等于空放行，否则跳转到登录界面response.sendDirster("login.html")
        User u = (User) request.getSession().getAttribute("u");
        //如果u==null，则未登录
        if(u==null){
            response.sendRedirect("/login.html");//跳转到登录界面，重新登录
        }else{//u!=null，则登录成功，不拦截
            chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
